2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
9 namespace SuperPolarity
13 protected SuperPolarity game;
15 public List<Actor> Children;
18 protected Texture2D Texture;
19 protected Vector2 Origin;
22 public Vector4 BoxDimensions;
23 protected Texture2D BoxTexture;
25 // Physical Properties
26 public Vector2 Position;
27 protected Vector2 Velocity;
28 protected Vector2 Acceleration;
31 // Constraints / Behavior
32 public float MaxVelocity;
33 protected float AccelerationRate;
35 protected bool Immortal;
38 protected Color Color;
44 get { return Texture.Width; }
49 get { return Texture.Height; }
52 public Actor(SuperPolarity newGame)
61 public virtual void Initialize(Texture2D texture, Vector2 position)
67 Children = new List<Actor>();
69 Origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
70 Velocity = new Vector2(0, 0);
71 Acceleration = new Vector2(0, 0);
74 AccelerationRate = 10;
83 BoxTexture = new Texture2D(game.GraphicsDevice, 1, 1);
84 BoxTexture.SetData(new Color[] { Color.White });
89 protected void InitBox()
91 Box = new Rectangle((int)(Position.X - BoxDimensions.X), (int)(Position.Y - BoxDimensions.X), (int)(BoxDimensions.X + BoxDimensions.X + BoxDimensions.W), (int)(BoxDimensions.Y + BoxDimensions.Y + BoxDimensions.Z));
94 public void AutoDeccelerate(GameTime gameTime)
96 if (Acceleration.X == 0 && Velocity.X > 0)
98 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X)
105 Acceleration.X = -AccelerationRate;
109 if (Acceleration.X == 0 && Velocity.X < 0)
111 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X)
118 Acceleration.X = AccelerationRate;
122 if (Acceleration.Y == 0 && Velocity.Y > 0)
124 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y)
131 Acceleration.Y = -AccelerationRate;
135 if (Acceleration.Y == 0 && Velocity.Y < 0)
137 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y)
144 Acceleration.Y = AccelerationRate;
149 public virtual void Update(GameTime gameTime)
157 protected virtual void UpdateBox()
159 Box.X = (int)(Position.X - BoxDimensions.X);
160 Box.Y = (int)(Position.Y - BoxDimensions.Y);
163 public virtual void Move(GameTime gameTime)
165 AutoDeccelerate(gameTime);
167 var maxVelocity = MaxVelocity;
169 Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
170 Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
172 if (Velocity.X > MaxVelocity)
174 Velocity.X = MaxVelocity;
177 if (Velocity.X < -MaxVelocity)
179 Velocity.X = -MaxVelocity;
182 if (Velocity.Y > MaxVelocity)
184 Velocity.Y = MaxVelocity;
187 if (Velocity.Y < -MaxVelocity)
189 Velocity.Y = -MaxVelocity;
192 Position.X = Position.X + Velocity.X;
193 Position.Y = Position.Y + Velocity.Y;
196 public void ChangeAngle()
198 if (Math.Abs(Velocity.Y) <= 0.1 && Math.Abs(Velocity.X) <= 0.1)
202 Angle = (float)Math.Atan2(Velocity.Y, Velocity.X);
205 public virtual void Draw(SpriteBatch spriteBatch)
207 foreach (Actor child in Children)
209 child.Draw(spriteBatch);
212 spriteBatch.Draw(Texture, Position, null, Color, Angle, Origin, 1f, SpriteEffects.None, 0f);
213 spriteBatch.Draw(BoxTexture, Box, new Color(255, 0, 255, 25));
218 for (var i = Children.Count; i > 0; i--)
220 var actor = Children[i - 1];
221 if (actor.Position.X < -SuperPolarity.OutlierBounds || actor.Position.Y < -SuperPolarity.OutlierBounds ||
222 actor.Position.X > game.GraphicsDevice.Viewport.Width + SuperPolarity.OutlierBounds ||
223 actor.Position.Y > game.GraphicsDevice.Viewport.Height + SuperPolarity.OutlierBounds)
225 Children.Remove(actor);
230 public virtual void Collide(Actor other, Rectangle collision)
234 public void TakeDamage(int amount)
246 protected virtual void Die()